#
# @lc app=leetcode.cn id=738 lang=python
#
# [738] 单调递增的数字
#

# @lc code=start
class Solution(object):
    def monotoneIncreasingDigits(self, n):
        """
        :type n: int
        :rtype: int
        """
        num_list = list(str(n))
        for i in range(len(num_list)-2, -1, -1):
            if num_list[i] > num_list[i+1]:
                num_list[i] = str(int(num_list[i])-1)
                
                for j in range(i+1, len(num_list)):
                    num_list[j] = '9' 
        return int(''.join(num_list))
        
# @lc code=end

